home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 21 / AACD 21.iso / AACD / Utilities / Ghostscript / src / gxdht.h < prev    next >
Encoding:
C/C++ Source or Header  |  2001-01-01  |  12.9 KB  |  316 lines

  1. /* Copyright (C) 1995, 1996, 1997, 1999, 2000 Aladdin Enterprises.  All rights reserved.
  2.   
  3.   This file is part of AFPL Ghostscript.
  4.   
  5.   AFPL Ghostscript is distributed with NO WARRANTY OF ANY KIND.  No author or
  6.   distributor accepts any responsibility for the consequences of using it, or
  7.   for whether it serves any particular purpose or works at all, unless he or
  8.   she says so in writing.  Refer to the Aladdin Free Public License (the
  9.   "License") for full details.
  10.   
  11.   Every copy of AFPL Ghostscript must include a copy of the License, normally
  12.   in a plain ASCII text file named PUBLIC.  The License grants you the right
  13.   to copy, modify and redistribute AFPL Ghostscript, but only under certain
  14.   conditions described in the License.  Among other things, the License
  15.   requires that the copyright notice and this notice be preserved on all
  16.   copies.
  17. */
  18.  
  19. /*$Id: gxdht.h,v 1.4 2000/09/19 19:00:36 lpd Exp $ */
  20. /* Definition of device halftones */
  21.  
  22. #ifndef gxdht_INCLUDED
  23. #  define gxdht_INCLUDED
  24.  
  25. #include "gsrefct.h"
  26. #include "gscsepnm.h"
  27. #include "gsmatrix.h"
  28. #include "gxarith.h"        /* for igcd */
  29. #include "gxhttype.h"
  30.  
  31. /*
  32.  * We represent a halftone tile as a rectangular super-cell consisting of
  33.  * multiple copies of a multi-cell whose corners lie on integral
  34.  * coordinates, which in turn is a parallelogram (normally square) array of
  35.  * basic parallelogram (normally square) cells whose corners lie on rational
  36.  * coordinates.
  37.  *
  38.  * Let T be the aspect ratio (ratio of physical pixel height to physical
  39.  * pixel width), which is abs(xx/yy) for portrait devices and abs(yx/xy) for
  40.  * landscape devices.  We characterize the basic cell by four rational
  41.  * numbers U(') = M(')/R(') and V(') = N(')/R(') where R(') is positive, at
  42.  * least one of U and V (and the corresponding one of U' and V') is
  43.  * non-zero, and U' is approximately U/T and V' is approximately V*T; these
  44.  * numbers define the vertices of the basic cell at device space coordinates
  45.  * (0,0), (U,V), (U-V',U'+V), and (-V',U'); then the multi-cell is defined
  46.  * similarly by M(') and N(').  From these definitions, the basic cell has
  47.  * an area of B = U*U' + V*V' = (M*M' + N*N') / R*R' pixels, and the
  48.  * multi-cell has an area of C = B * R*R' = M*M' + N*N' pixels.
  49.  *
  50.  * If the coefficients of the default device transformation matrix are xx,
  51.  * xy, yx, and yy, then U and V are related to the frequency F and the angle
  52.  * A by:
  53.  *      P = 72 / F;
  54.  *      U = P * (xx * cos(A) + yx * sin(A));
  55.  *      V = P * (xy * cos(A) + yy * sin(A)).
  56.  *
  57.  * We can tile the plane with any rectangular super-cell that consists of
  58.  * repetitions of the multi-cell and whose corners coincide with multi-cell
  59.  * coordinates (0,0).  We observe that for any integers i, j such that i*N -
  60.  * j*M' = 0, a multi-cell corner lies on the X axis at W = i*M + j*N';
  61.  * similarly, if i'*M - j'*N' = 0, a corner lies on the Y axis at W' = i'*N
  62.  * + j'*M'.  Then the super-cell occupies Z = W * W' pixels, consisting of Z
  63.  * / C multi-cells or Z / B basic cells.  The trick in all this is to find
  64.  * values of F and A that aren't too far from the requested ones, and that
  65.  * yield a manageably small value for Z.
  66.  *
  67.  * Note that the super-cell only has to be so large because we want to use
  68.  * it directly to tile the plane.  In fact, we can decompose it into W' / D
  69.  * horizontal strips of width W and height D, shifted horizontally with
  70.  * respect to each other by S pixels, where we compute S by finding h and k
  71.  * such that h*N - k*M' = D and then S = h*M + k*N'.  The halftone setup
  72.  * routines only generate a single strip of samples, and let
  73.  * gx_ht_construct_spot_order construct the rest.  If W' is large, we
  74.  * actually keep only one strip, and let the strip_tile_rectangle routines
  75.  * do the shifting at rendering time.
  76.  */
  77. typedef struct gx_ht_cell_params_s {
  78.     /* Defining values.  M * M1 != 0 or N * N1 != 0; R > 0, R1 > 0. */
  79.     /* R and D are short rather than ushort so that we don't get */
  80.     /* unsigned results from arithmetic. */
  81.     short M, N, R;
  82.     short M1, N1, R1;
  83.     /* Derived values. */
  84.     ulong C;
  85.     short D, D1;
  86.     uint W, W1;
  87.     int S;
  88. } gx_ht_cell_params_t;
  89.  
  90. /* Compute the derived values from the defining values. */
  91. void gx_compute_cell_values(P1(gx_ht_cell_params_t *));
  92.  
  93. /*
  94.  * The whitening order is represented by a pair of arrays.
  95.  * The levels array contains an integer (an index into the bit_data array)
  96.  * for each distinct halftone level, indicating how many pixels should be
  97.  * whitened for that level; levels[0] = 0, levels[i] <= levels[i+1], and
  98.  * levels[num_levels-1] <= num_bits.  The bit_data array contains data to
  99.  * specify which bits should be set for each level: it has several
  100.  * different representations depending on space/time tradeoffs.
  101.  *
  102.  * The default bit_data representation is an (offset,mask) pair for each
  103.  * pixel in the tile.  bits[i].offset is the (properly aligned) byte index
  104.  * of a pixel in the tile; bits[i].mask is the mask to be or'ed into this
  105.  * byte and following ones.  This is arranged so it will work properly on
  106.  * either big- or little-endian machines, and with different mask widths.
  107.  */
  108. /*
  109.  * The mask width must be at least as wide as uint,
  110.  * and must not be wider than the width implied by align_bitmap_mod.
  111.  */
  112. typedef uint ht_mask_t;
  113.  
  114. #define ht_mask_bits (sizeof(ht_mask_t) * 8)
  115. typedef struct gx_ht_bit_s {
  116.     uint offset;
  117.     ht_mask_t mask;
  118. } gx_ht_bit;
  119.  
  120. /* During sampling, bits[i].mask is used to hold a normalized sample value. */
  121. typedef ht_mask_t ht_sample_t;
  122.  
  123. /* The following awkward expression avoids integer overflow. */
  124. #define max_ht_sample (ht_sample_t)(((1 << (ht_mask_bits - 2)) - 1) * 2 + 1)
  125.  
  126. /*
  127.  * Define the internal representation of a halftone order.
  128.  * Note that it may include a cached transfer function.
  129.  *
  130.  * Halftone orders exist in two slightly different configurations, strip and
  131.  * complete.  In a complete order, shift = 0 and full_height = height; in a
  132.  * strip order, shift != 0 and full_height is the height of a fully expanded
  133.  * halftone made up of enough shifted strip copies to get back to a zero
  134.  * shift.  In other words, full_height is a cached value, but it is an
  135.  * important one, since it is the modulus used for computing the
  136.  * tile-relative phase.  Requirements:
  137.  *      width > 0, height > 0, multiple > 0
  138.  *      raster >= bitmap_raster(width)
  139.  *      0 <= shift < width
  140.  *      num_bits = width * height
  141.  * For complete orders:
  142.  *      full_height = height
  143.  * For strip orders:
  144.  *      full_height = height * width / gcd(width, shift)
  145.  * Note that during the sampling of a complete spot halftone, these
  146.  * invariants may be violated; in particular, it is possible that shift != 0
  147.  * and height < full_height, even though num_bits and num_levels reflect the
  148.  * full height.  In this case, the invariant is restored (by resetting
  149.  * shift and height) when sampling is finished.  However, we must save the
  150.  * original height and shift values used for sampling, since sometimes we
  151.  * run the "finishing" routines more than once.  (This is ugly, but it's
  152.  * too hard to fix.)
  153.  *
  154.  * See gxbitmap.h for more details about strip halftones.
  155.  */
  156. typedef struct gx_ht_cache_s gx_ht_cache;
  157. #ifndef gx_ht_order_DEFINED
  158. #  define gx_ht_order_DEFINED
  159. typedef struct gx_ht_order_s gx_ht_order;
  160. #endif
  161. #ifndef gx_ht_tile_DEFINED
  162. #  define gx_ht_tile_DEFINED
  163. typedef struct gx_ht_tile_s gx_ht_tile;
  164. #endif
  165. typedef struct gx_ht_order_procs_s {
  166.  
  167.     /* Define the size of each element of bit_data. */
  168.  
  169.     uint bit_data_elt_size;
  170.  
  171.     /* Construct the order from the threshold array. */
  172.     /* Note that for 16-bit threshold values, */
  173.     /* each value is 2 bytes in big-endian order (Adobe spec). */
  174.  
  175.     int (*construct_order)(P2(gx_ht_order *order, const byte *thresholds));
  176.  
  177.     /* Return the (x,y) coordinate of an element of bit_data. */
  178.  
  179.     int (*bit_index)(P3(const gx_ht_order *order, uint index,
  180.             gs_int_point *ppt));
  181.  
  182.     /* Update a halftone cache tile to match this order. */
  183.  
  184.     int (*render)(P3(gx_ht_tile *tile, int new_bit_level,
  185.              const gx_ht_order *order));
  186.  
  187. } gx_ht_order_procs_t;
  188. /*
  189.  * Define the procedure vectors for the supported implementations
  190.  * (in gxhtbit.c).
  191.  */
  192. extern const gx_ht_order_procs_t ht_order_procs_table[2];
  193. #define ht_order_procs_default ht_order_procs_table[0]    /* bit_data is gx_ht_bit[] */
  194. #define ht_order_procs_short ht_order_procs_table[1]    /* bit_data is ushort[] */
  195. /* For screen/spot halftones, we must record additional parameters. */
  196. typedef struct gx_ht_order_screen_params_s {
  197.     gs_matrix matrix;        /* CTM when the function was sampled */
  198.     ulong max_size;        /* max bitmap size */
  199. } gx_ht_order_screen_params_t;
  200. struct gx_ht_order_s {
  201.     gx_ht_cell_params_t params;    /* parameters defining the cells */
  202.     ushort width;
  203.     ushort height;
  204.     ushort raster;
  205.     ushort shift;
  206.     ushort orig_height;
  207.     ushort orig_shift;
  208.     uint full_height;
  209.     uint num_levels;        /* = levels size */
  210.     uint num_bits;        /* = countof(bit_data) = width * height */
  211.     const gx_ht_order_procs_t *procs;
  212.     gs_memory_t *data_memory;    /* for levels and bit_data, may be 0 */
  213.     uint *levels;
  214.     void *bit_data;
  215.     gx_ht_cache *cache;        /* cache to use */
  216.     gx_transfer_map *transfer;    /* TransferFunction or 0 */
  217.     gx_ht_order_screen_params_t screen_params;
  218. };
  219.  
  220. #define ht_order_is_complete(porder)\
  221.   ((porder)->shift == 0)
  222. #define ht_order_full_height(porder)\
  223.   ((porder)->shift == 0 ? (porder)->height :\
  224.    (porder)->width / igcd((porder)->width, (porder)->shift) *\
  225.      (porder)->height)
  226.  
  227. /* We only export st_ht_order for use in st_screen_enum. */
  228. extern_st(st_ht_order);
  229. #define public_st_ht_order()    /* in gsht.c */\
  230.   gs_public_st_composite(st_ht_order, gx_ht_order, "gx_ht_order",\
  231.     ht_order_enum_ptrs, ht_order_reloc_ptrs)
  232. #define st_ht_order_max_ptrs 4
  233.  
  234. /*
  235.  * Define a device halftone.  This consists of one or more orders.
  236.  * If components = 0, then order is the only current halftone screen
  237.  * (set by setscreen, Type 1 sethalftone, Type 3 sethalftone, or
  238.  * Type 5 sethalftone with only a Default).  Otherwise, order is the
  239.  * gray or black screen (for gray/RGB or CMYK devices respectively),
  240.  * and components is an array of gx_ht_order_components parallel to
  241.  * the components of the client halftone (set by setcolorscreen or
  242.  * Type 5 sethalftone).
  243.  *
  244.  * Multi-component halftone orders may be required even in Level 1 systems,
  245.  * because they are needed for setcolorscreen.
  246.  *
  247.  * NOTE: it is assumed that all subsidiary structures of device halftones
  248.  * (the components array, and the bit_data, levels, cache, and transfer
  249.  * members of any gx_ht_orders, both the default order and any component
  250.  * orders) are allocated with the same allocator as the device halftone
  251.  * itself.
  252.  */
  253. typedef struct gx_ht_order_component_s {
  254.     gx_ht_order corder;
  255.     gs_ht_separation_name cname;
  256. } gx_ht_order_component;
  257.  
  258. #define private_st_ht_order_component()    /* in gsht.c */\
  259.   gs_private_st_ptrs_add0(st_ht_order_component, gx_ht_order_component,\
  260.     "gx_ht_order_component", ht_order_component_enum_ptrs,\
  261.      ht_order_component_reloc_ptrs, st_ht_order, corder)
  262. #define st_ht_order_component_max_ptrs st_ht_order_max_ptrs
  263. /* We only export st_ht_order_component_element for use in banding. */
  264. extern_st(st_ht_order_component_element);
  265. #define public_st_ht_order_comp_element() /* in gsht.c */\
  266.   gs_public_st_element(st_ht_order_component_element, gx_ht_order_component,\
  267.     "gx_ht_order_component[]", ht_order_element_enum_ptrs,\
  268.     ht_order_element_reloc_ptrs, st_ht_order_component)
  269.  
  270. #ifndef gx_device_halftone_DEFINED
  271. #  define gx_device_halftone_DEFINED
  272. typedef struct gx_device_halftone_s gx_device_halftone;
  273. #endif
  274.  
  275. /*
  276.  * color_indices is a cache that gives the indices in components of
  277.  * the screens for the 1, 3, or 4 primary color(s).  These indices are
  278.  * always in the same order, namely:
  279.  *      -,-,-,W(gray)
  280.  *      R,G,B,-
  281.  *      C,M,Y,K
  282.  */
  283. struct gx_device_halftone_s {
  284.     gx_ht_order order;        /* must be first, for subclassing */
  285.     rc_header rc;
  286.     gs_id id;            /* the id changes whenever the data change */
  287.     /*
  288.      * We have to keep the halftone type so that we can pass it
  289.      * through the band list for gx_imager_dev_ht_install.
  290.      */
  291.     gs_halftone_type type;
  292.     gx_ht_order_component *components;
  293.     uint num_comp;
  294.     /* The following are computed from the above. */
  295.     uint color_indices[4];
  296.     int lcm_width, lcm_height;    /* LCM of primary color tile sizes, */
  297.     /* max_int if overflowed */
  298. };
  299.  
  300. extern_st(st_device_halftone);
  301. #define public_st_device_halftone() /* in gsht.c */\
  302.   gs_public_st_ptrs_add1(st_device_halftone, gx_device_halftone,\
  303.     "gx_device_halftone", device_halftone_enum_ptrs,\
  304.     device_halftone_reloc_ptrs, st_ht_order, order, components)
  305. #define st_device_halftone_max_ptrs (st_ht_order_max_ptrs + 1)
  306.  
  307. /* Complete a halftone order defined by a threshold array. */
  308. void gx_ht_complete_threshold_order(P1(gx_ht_order *porder));
  309.  
  310. /* Release a gx_device_halftone by freeing its components. */
  311. /* (Don't free the gx_device_halftone itself.) */
  312. void gx_device_halftone_release(P2(gx_device_halftone * pdht,
  313.                    gs_memory_t * mem));
  314.  
  315. #endif /* gxdht_INCLUDED */
  316.